import Singleton from "../base/Singleton"
import EventMgr from "../manager/EventMgr"
import { SdkEvent, SdkName, SdkRewardVideoState } from "./SdkConstants"
import SdkUtil from "./SdkUtil"
import SdkBase from "./platform/SdkBase"
import SdkBytedance from "./platform/SdkBytedance"
import SdkOppo from "./platform/SdkOppo"
import SdkWechat from "./platform/SdkWechat"


export default class SdkMgr extends Singleton {
    public static get inst() {
        return this.getInstance<SdkMgr>()
    }


    private conf: ISdkConf
    private platform: SdkBase

    private _rewardVideoIdx: number = 0


    public init(confs: { [platform: string]: ISdkConf }, shareCallBack?: Function) {
        if (SdkUtil.isBytedance) {
            this.conf = confs[SdkName.bytedance]
            this.platform = new SdkBytedance(this.conf)
        } else if (SdkUtil.isWechat) {
            this.conf = confs[SdkName.wechat]
            this.platform = new SdkWechat(shareCallBack)
        } else if (SdkUtil.isOppo) {
            this.conf = confs[SdkName.oppo]
            this.platform = new SdkOppo()
        } else {
            console.error('SdkMgr.init 未知平台')
        }
    }


    public getLaunchOptionsSync(): Object {
        return this.platform?.getLaunchOptionsSync()
    }

    public share(title?: string, imageUrl?: string, imageUrlId?: string, query?: string) {
        this.platform?.share(title, imageUrl, imageUrlId, query)
    }


    public rewardVideoShow(onComplete: (flag: boolean) => void) {
        if (!this.platform) {
            onComplete(true)
            return
        }

        const ids = this.conf.rewardVideos
        if (!ids || ids.length <= 0) {
            console.error('SdkMgr.rewardVideoShow 未配置广告 Id')
            onComplete(true)
            return
        }

        const id = ids[this._rewardVideoIdx % ids.length]
        this._rewardVideoIdx++

        this.platform.rewardVideoPlay(id, (state: number, ret: any) => {
            switch (state) {
                case SdkRewardVideoState.PlaySuccess:
                    EventMgr.emit(SdkEvent.RewardVideoShowing)
                    break
                case SdkRewardVideoState.PlayError:
                    EventMgr.emit(SdkEvent.Error, ret)

                    const errMsg: string = ret
                    onComplete(errMsg.indexOf('审核') != -1)

                    EventMgr.emit(SdkEvent.RewardVideoShowOver)
                    break
                case SdkRewardVideoState.Close:
                    onComplete(false)
                    EventMgr.emit(SdkEvent.RewardVideoShowOver)
                    break
                case SdkRewardVideoState.CloseReward:
                    onComplete(true)
                    EventMgr.emit(SdkEvent.RewardVideoShowOver)
                    break
            }
        })

        EventMgr.emit(SdkEvent.RewardVideoShowPrepare)
    }


    public get recordIsSupport(): boolean {
        return this.platform?.recordIsSupport
    }

    public recordStart(duration: number = 20, stopCb?: Function) {
        this.platform?.recordStart(duration, stopCb)
    }

    public recordStop() {
        this.platform?.recordStop()
    }

    public recordPause() {
        this.platform?.recordPause()
    }

    public recordResume() {
        this.platform?.recordResume()
    }

    public recordShare(needTime: number = 5, param?: Object) {
        const msg = this.platform?.recordShare(needTime, param)
        if (!msg) return

        EventMgr.emit(SdkEvent.Error, msg)
    }


    public vibrateShort(type: string = 'medium') {
        this.platform?.vibrateShort(type)
    }

    public vibrateLong() {
        this.platform?.vibrateLong()
    }


    /**
     * 字节小游戏环境 production:线上版(审核版本也是该值) | development:测试版 | preview:预览版 | gray:灰度版
     */
    public get bytedanceEnvType(): string {
        return this.platform?.bdEnvType
    }
}
